-
Notifications
You must be signed in to change notification settings - Fork 232
Make can::Id
usable as key in BTreeMap
and HashMap
#384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…`Ord` and `Hash`
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @therealprof (or someone else) soon. Please see the contribution instructions for more information. |
I think having |
Does that mean that |
In that case, would it not make more sense if the ordering was the same for all three types? |
Unfortunately arbitration rules are a bit more complex than that if I understand the CAN specification correctly. Example: |
Oh, ok thanks |
So something more like impl Ord for Id {
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
let split_id = |id: &Id| {
let (standard_id_part, ide_bit, extended_id_part) = match id {
Id::Standard(StandardId(x)) => (*x, 0, 0),
Id::Extended(x) => (
x.standard_id().0,
1,
x.0 & ((1 << 18) - 1), // Bit ID-17 to ID-0
),
};
(standard_id_part, ide_bit, extended_id_part)
};
split_id(self).cmp(&split_id(other))
}
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good idea, thank you!
I have not looked in detail but a couple of things that would be necessary for the merge would be:
- Documentation on how to use this comparison and how it behaves with regard to the CAN arbitration rules and so on.
- An entry in the changelog about this.
Looks good to me now. Can you please fix the two tests too:
|
I hope I have put the documentation in the right place. Otherwise please let me know. The failing test have been removed, they were forgotten left overs from my first attempt |
Anything more I can do? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, just a couple of tiny nitpicks that I can merge myself.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much @usbalbin and @timokroeger!
bors r+
Implement
Ord
andHash
forcan::Id
to make it usable as key inBTreeMap
andHashMap